import { FileItem } from '../../file_manager/FileItem';
import { LogLevel, LogTarget } from './LogBasedata';
import { LogEvent } from './LogEvent';
import { LogTargetConsole } from './log_target/LogTargetConsole';
import { LogTargetDailyRollingFile } from './log_target/LogTargetDailyRollingFile';

/**
 * 分别管理一组  Console,File的对象.
 */
export class Logger {
  consoleTarget: LogTargetConsole;
  fileTarget: LogTargetDailyRollingFile;
  logLevel: LogLevel

  constructor(logDir: string, initialContent: string) {
    this.logLevel = LogLevel.Verbose;
    this.consoleTarget = new LogTargetConsole();
    this.fileTarget = new LogTargetDailyRollingFile(logDir, 30, initialContent);
  }

  log(logEvent: LogEvent) {
    if (this.canLogEvent(logEvent)) {
      this.logEvent(logEvent);
    }
  }

  canLogEvent(logEvent: LogEvent): boolean {
    if (logEvent.level < this.logLevel) {
      return false;
    }
    return true;
  }

  logEvent(logEvent: LogEvent) {
    if (logEvent.target & LogTarget.Console) {
      this.consoleTarget.log(logEvent);
    }
    if (logEvent.target & LogTarget.File) {
      this.fileTarget.log(logEvent);
    }
  }

  getLogs(): string[] {
    return this.fileTarget.getLogs();
  }
}

